{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f55808d7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Init Plugin\n",
      "Init Graph Optimizer\n",
      "Init Kernel\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow import keras \n",
    "from tensorflow.keras import layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3c0b80df",
   "metadata": {},
   "outputs": [],
   "source": [
    "data= keras.datasets.imdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4d5f9b57",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<__array_function__ internals>:5: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
      "/Users/yuconggen/miniforge3/lib/python3.9/site-packages/tensorflow/python/keras/datasets/imdb.py:155: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
      "  x_train, y_train = np.array(xs[:idx]), np.array(labels[:idx])\n",
      "/Users/yuconggen/miniforge3/lib/python3.9/site-packages/tensorflow/python/keras/datasets/imdb.py:156: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n",
      "  x_test, y_test = np.array(xs[idx:]), np.array(labels[idx:])\n"
     ]
    }
   ],
   "source": [
    "(x_train,y_train),(x_test,y_test)=data.load_data(num_words=10000)#MAX_word=10000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8889ac2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(25000,)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c54c4c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 14,\n",
       " 22,\n",
       " 16,\n",
       " 43,\n",
       " 530,\n",
       " 973,\n",
       " 1622,\n",
       " 1385,\n",
       " 65,\n",
       " 458,\n",
       " 4468,\n",
       " 66,\n",
       " 3941,\n",
       " 4,\n",
       " 173,\n",
       " 36,\n",
       " 256,\n",
       " 5,\n",
       " 25,\n",
       " 100,\n",
       " 43,\n",
       " 838,\n",
       " 112,\n",
       " 50,\n",
       " 670,\n",
       " 2,\n",
       " 9,\n",
       " 35,\n",
       " 480,\n",
       " 284,\n",
       " 5,\n",
       " 150,\n",
       " 4,\n",
       " 172,\n",
       " 112,\n",
       " 167,\n",
       " 2,\n",
       " 336,\n",
       " 385,\n",
       " 39,\n",
       " 4,\n",
       " 172,\n",
       " 4536,\n",
       " 1111,\n",
       " 17,\n",
       " 546,\n",
       " 38,\n",
       " 13,\n",
       " 447,\n",
       " 4,\n",
       " 192,\n",
       " 50,\n",
       " 16,\n",
       " 6,\n",
       " 147,\n",
       " 2025,\n",
       " 19,\n",
       " 14,\n",
       " 22,\n",
       " 4,\n",
       " 1920,\n",
       " 4613,\n",
       " 469,\n",
       " 4,\n",
       " 22,\n",
       " 71,\n",
       " 87,\n",
       " 12,\n",
       " 16,\n",
       " 43,\n",
       " 530,\n",
       " 38,\n",
       " 76,\n",
       " 15,\n",
       " 13,\n",
       " 1247,\n",
       " 4,\n",
       " 22,\n",
       " 17,\n",
       " 515,\n",
       " 17,\n",
       " 12,\n",
       " 16,\n",
       " 626,\n",
       " 18,\n",
       " 2,\n",
       " 5,\n",
       " 62,\n",
       " 386,\n",
       " 12,\n",
       " 8,\n",
       " 316,\n",
       " 8,\n",
       " 106,\n",
       " 5,\n",
       " 4,\n",
       " 2223,\n",
       " 5244,\n",
       " 16,\n",
       " 480,\n",
       " 66,\n",
       " 3785,\n",
       " 33,\n",
       " 4,\n",
       " 130,\n",
       " 12,\n",
       " 16,\n",
       " 38,\n",
       " 619,\n",
       " 5,\n",
       " 25,\n",
       " 124,\n",
       " 51,\n",
       " 36,\n",
       " 135,\n",
       " 48,\n",
       " 25,\n",
       " 1415,\n",
       " 33,\n",
       " 6,\n",
       " 22,\n",
       " 12,\n",
       " 215,\n",
       " 28,\n",
       " 77,\n",
       " 52,\n",
       " 5,\n",
       " 14,\n",
       " 407,\n",
       " 16,\n",
       " 82,\n",
       " 2,\n",
       " 8,\n",
       " 4,\n",
       " 107,\n",
       " 117,\n",
       " 5952,\n",
       " 15,\n",
       " 256,\n",
       " 4,\n",
       " 2,\n",
       " 7,\n",
       " 3766,\n",
       " 5,\n",
       " 723,\n",
       " 36,\n",
       " 71,\n",
       " 43,\n",
       " 530,\n",
       " 476,\n",
       " 26,\n",
       " 400,\n",
       " 317,\n",
       " 46,\n",
       " 7,\n",
       " 4,\n",
       " 2,\n",
       " 1029,\n",
       " 13,\n",
       " 104,\n",
       " 88,\n",
       " 4,\n",
       " 381,\n",
       " 15,\n",
       " 297,\n",
       " 98,\n",
       " 32,\n",
       " 2071,\n",
       " 56,\n",
       " 26,\n",
       " 141,\n",
       " 6,\n",
       " 194,\n",
       " 7486,\n",
       " 18,\n",
       " 4,\n",
       " 226,\n",
       " 22,\n",
       " 21,\n",
       " 134,\n",
       " 476,\n",
       " 26,\n",
       " 480,\n",
       " 5,\n",
       " 144,\n",
       " 30,\n",
       " 5535,\n",
       " 18,\n",
       " 51,\n",
       " 36,\n",
       " 28,\n",
       " 224,\n",
       " 92,\n",
       " 25,\n",
       " 104,\n",
       " 4,\n",
       " 226,\n",
       " 65,\n",
       " 16,\n",
       " 38,\n",
       " 1334,\n",
       " 88,\n",
       " 12,\n",
       " 16,\n",
       " 283,\n",
       " 5,\n",
       " 16,\n",
       " 4472,\n",
       " 113,\n",
       " 103,\n",
       " 32,\n",
       " 15,\n",
       " 16,\n",
       " 5345,\n",
       " 19,\n",
       " 178,\n",
       " 32]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5af191ea",
   "metadata": {},
   "source": [
    "文本训练成密集向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e4d447a9",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[218,\n",
       " 189,\n",
       " 141,\n",
       " 550,\n",
       " 147,\n",
       " 43,\n",
       " 123,\n",
       " 562,\n",
       " 233,\n",
       " 130,\n",
       " 450,\n",
       " 99,\n",
       " 117,\n",
       " 238,\n",
       " 109,\n",
       " 129,\n",
       " 163,\n",
       " 752,\n",
       " 212,\n",
       " 177,\n",
       " 129,\n",
       " 140,\n",
       " 256,\n",
       " 888,\n",
       " 93,\n",
       " 142,\n",
       " 220,\n",
       " 193,\n",
       " 171,\n",
       " 221,\n",
       " 174,\n",
       " 647,\n",
       " 233,\n",
       " 162,\n",
       " 597,\n",
       " 234,\n",
       " 51,\n",
       " 336,\n",
       " 139,\n",
       " 231,\n",
       " 704,\n",
       " 142,\n",
       " 861,\n",
       " 132,\n",
       " 122,\n",
       " 570,\n",
       " 55,\n",
       " 214,\n",
       " 103,\n",
       " 186,\n",
       " 113,\n",
       " 169,\n",
       " 469,\n",
       " 138,\n",
       " 302,\n",
       " 766,\n",
       " 351,\n",
       " 146,\n",
       " 59,\n",
       " 206,\n",
       " 107,\n",
       " 152,\n",
       " 186,\n",
       " 431,\n",
       " 147,\n",
       " 684,\n",
       " 383,\n",
       " 324,\n",
       " 252,\n",
       " 263,\n",
       " 787,\n",
       " 211,\n",
       " 314,\n",
       " 118,\n",
       " 390,\n",
       " 132,\n",
       " 710,\n",
       " 306,\n",
       " 167,\n",
       " 115,\n",
       " 95,\n",
       " 158,\n",
       " 156,\n",
       " 82,\n",
       " 502,\n",
       " 314,\n",
       " 190,\n",
       " 174,\n",
       " 60,\n",
       " 145,\n",
       " 214,\n",
       " 659,\n",
       " 408,\n",
       " 515,\n",
       " 461,\n",
       " 202,\n",
       " 238,\n",
       " 170,\n",
       " 107,\n",
       " 171,\n",
       " 158,\n",
       " 145,\n",
       " 790,\n",
       " 258,\n",
       " 287,\n",
       " 67,\n",
       " 123,\n",
       " 975,\n",
       " 775,\n",
       " 236,\n",
       " 195,\n",
       " 274,\n",
       " 214,\n",
       " 91,\n",
       " 1038,\n",
       " 815,\n",
       " 183,\n",
       " 206,\n",
       " 50,\n",
       " 118,\n",
       " 147,\n",
       " 141,\n",
       " 60,\n",
       " 56,\n",
       " 439,\n",
       " 439,\n",
       " 213,\n",
       " 144,\n",
       " 533,\n",
       " 303,\n",
       " 203,\n",
       " 563,\n",
       " 129,\n",
       " 153,\n",
       " 55,\n",
       " 92,\n",
       " 174,\n",
       " 187,\n",
       " 183,\n",
       " 165,\n",
       " 78,\n",
       " 198,\n",
       " 156,\n",
       " 223,\n",
       " 127,\n",
       " 61,\n",
       " 362,\n",
       " 84,\n",
       " 57,\n",
       " 176,\n",
       " 159,\n",
       " 57,\n",
       " 159,\n",
       " 165,\n",
       " 213,\n",
       " 194,\n",
       " 149,\n",
       " 130,\n",
       " 203,\n",
       " 19,\n",
       " 98,\n",
       " 466,\n",
       " 525,\n",
       " 130,\n",
       " 322,\n",
       " 153,\n",
       " 408,\n",
       " 215,\n",
       " 472,\n",
       " 143,\n",
       " 136,\n",
       " 354,\n",
       " 260,\n",
       " 319,\n",
       " 125,\n",
       " 209,\n",
       " 282,\n",
       " 810,\n",
       " 142,\n",
       " 240,\n",
       " 148,\n",
       " 198,\n",
       " 193,\n",
       " 123,\n",
       " 128,\n",
       " 103,\n",
       " 479,\n",
       " 345,\n",
       " 263,\n",
       " 165,\n",
       " 205,\n",
       " 333,\n",
       " 184,\n",
       " 92,\n",
       " 177,\n",
       " 335,\n",
       " 120,\n",
       " 121,\n",
       " 259,\n",
       " 180,\n",
       " 160,\n",
       " 114,\n",
       " 59,\n",
       " 343,\n",
       " 513,\n",
       " 133,\n",
       " 206,\n",
       " 152,\n",
       " 206,\n",
       " 572,\n",
       " 153,\n",
       " 139,\n",
       " 151,\n",
       " 129,\n",
       " 129,\n",
       " 196,\n",
       " 433,\n",
       " 199,\n",
       " 140,\n",
       " 311,\n",
       " 151,\n",
       " 200,\n",
       " 584,\n",
       " 127,\n",
       " 513,\n",
       " 781,\n",
       " 932,\n",
       " 526,\n",
       " 161,\n",
       " 646,\n",
       " 135,\n",
       " 52,\n",
       " 267,\n",
       " 174,\n",
       " 185,\n",
       " 219,\n",
       " 81,\n",
       " 219,\n",
       " 131,\n",
       " 153,\n",
       " 270,\n",
       " 644,\n",
       " 155,\n",
       " 546,\n",
       " 284,\n",
       " 85,\n",
       " 293,\n",
       " 155,\n",
       " 358,\n",
       " 45,\n",
       " 231,\n",
       " 124,\n",
       " 178,\n",
       " 118,\n",
       " 260,\n",
       " 393,\n",
       " 127,\n",
       " 157,\n",
       " 107,\n",
       " 322,\n",
       " 188,\n",
       " 126,\n",
       " 155,\n",
       " 294,\n",
       " 249,\n",
       " 177,\n",
       " 138,\n",
       " 215,\n",
       " 263,\n",
       " 132,\n",
       " 150,\n",
       " 217,\n",
       " 188,\n",
       " 385,\n",
       " 199,\n",
       " 127,\n",
       " 325,\n",
       " 161,\n",
       " 140,\n",
       " 215,\n",
       " 240,\n",
       " 230,\n",
       " 327,\n",
       " 129,\n",
       " 113,\n",
       " 225,\n",
       " 87,\n",
       " 496,\n",
       " 234,\n",
       " 311,\n",
       " 215,\n",
       " 111,\n",
       " 102,\n",
       " 110,\n",
       " 165,\n",
       " 839,\n",
       " 296,\n",
       " 130,\n",
       " 104,\n",
       " 274,\n",
       " 229,\n",
       " 235,\n",
       " 653,\n",
       " 468,\n",
       " 578,\n",
       " 139,\n",
       " 315,\n",
       " 65,\n",
       " 178,\n",
       " 836,\n",
       " 164,\n",
       " 239,\n",
       " 212,\n",
       " 297,\n",
       " 258,\n",
       " 157,\n",
       " 78,\n",
       " 544,\n",
       " 152,\n",
       " 120,\n",
       " 208,\n",
       " 163,\n",
       " 226,\n",
       " 304,\n",
       " 195,\n",
       " 454,\n",
       " 121,\n",
       " 175,\n",
       " 617,\n",
       " 320,\n",
       " 121,\n",
       " 245,\n",
       " 655,\n",
       " 114,\n",
       " 131,\n",
       " 104,\n",
       " 238,\n",
       " 138,\n",
       " 164,\n",
       " 52,\n",
       " 215,\n",
       " 87,\n",
       " 471,\n",
       " 142,\n",
       " 289,\n",
       " 106,\n",
       " 141,\n",
       " 239,\n",
       " 412,\n",
       " 154,\n",
       " 175,\n",
       " 828,\n",
       " 41,\n",
       " 144,\n",
       " 525,\n",
       " 176,\n",
       " 551,\n",
       " 251,\n",
       " 621,\n",
       " 159,\n",
       " 75,\n",
       " 207,\n",
       " 80,\n",
       " 94,\n",
       " 78,\n",
       " 449,\n",
       " 622,\n",
       " 157,\n",
       " 85,\n",
       " 260,\n",
       " 1011,\n",
       " 444,\n",
       " 326,\n",
       " 586,\n",
       " 118,\n",
       " 270,\n",
       " 360,\n",
       " 95,\n",
       " 640,\n",
       " 315,\n",
       " 138,\n",
       " 573,\n",
       " 434,\n",
       " 313,\n",
       " 128,\n",
       " 1007,\n",
       " 130,\n",
       " 257,\n",
       " 209,\n",
       " 159,\n",
       " 602,\n",
       " 109,\n",
       " 250,\n",
       " 117,\n",
       " 149,\n",
       " 199,\n",
       " 55,\n",
       " 578,\n",
       " 158,\n",
       " 323,\n",
       " 486,\n",
       " 177,\n",
       " 73,\n",
       " 127,\n",
       " 138,\n",
       " 130,\n",
       " 110,\n",
       " 283,\n",
       " 244,\n",
       " 375,\n",
       " 137,\n",
       " 156,\n",
       " 153,\n",
       " 112,\n",
       " 94,\n",
       " 137,\n",
       " 195,\n",
       " 137,\n",
       " 112,\n",
       " 25,\n",
       " 106,\n",
       " 105,\n",
       " 272,\n",
       " 857,\n",
       " 116,\n",
       " 58,\n",
       " 114,\n",
       " 71,\n",
       " 57,\n",
       " 292,\n",
       " 56,\n",
       " 157,\n",
       " 283,\n",
       " 87,\n",
       " 327,\n",
       " 481,\n",
       " 918,\n",
       " 129,\n",
       " 181,\n",
       " 214,\n",
       " 601,\n",
       " 215,\n",
       " 117,\n",
       " 112,\n",
       " 401,\n",
       " 165,\n",
       " 154,\n",
       " 587,\n",
       " 417,\n",
       " 345,\n",
       " 233,\n",
       " 496,\n",
       " 403,\n",
       " 317,\n",
       " 189,\n",
       " 340,\n",
       " 195,\n",
       " 370,\n",
       " 194,\n",
       " 150,\n",
       " 559,\n",
       " 48,\n",
       " 129,\n",
       " 110,\n",
       " 45,\n",
       " 173,\n",
       " 674,\n",
       " 416,\n",
       " 233,\n",
       " 146,\n",
       " 73,\n",
       " 146,\n",
       " 190,\n",
       " 130,\n",
       " 127,\n",
       " 217,\n",
       " 785,\n",
       " 227,\n",
       " 119,\n",
       " 149,\n",
       " 150,\n",
       " 175,\n",
       " 588,\n",
       " 130,\n",
       " 414,\n",
       " 172,\n",
       " 523,\n",
       " 119,\n",
       " 130,\n",
       " 122,\n",
       " 219,\n",
       " 264,\n",
       " 202,\n",
       " 218,\n",
       " 367,\n",
       " 230,\n",
       " 429,\n",
       " 985,\n",
       " 144,\n",
       " 128,\n",
       " 60,\n",
       " 78,\n",
       " 125,\n",
       " 131,\n",
       " 186,\n",
       " 283,\n",
       " 121,\n",
       " 226,\n",
       " 82,\n",
       " 57,\n",
       " 468,\n",
       " 336,\n",
       " 218,\n",
       " 110,\n",
       " 535,\n",
       " 603,\n",
       " 147,\n",
       " 117,\n",
       " 156,\n",
       " 72,\n",
       " 72,\n",
       " 69,\n",
       " 529,\n",
       " 148,\n",
       " 56,\n",
       " 263,\n",
       " 202,\n",
       " 348,\n",
       " 172,\n",
       " 104,\n",
       " 212,\n",
       " 146,\n",
       " 191,\n",
       " 251,\n",
       " 179,\n",
       " 722,\n",
       " 156,\n",
       " 141,\n",
       " 235,\n",
       " 97,\n",
       " 69,\n",
       " 222,\n",
       " 228,\n",
       " 233,\n",
       " 46,\n",
       " 130,\n",
       " 599,\n",
       " 146,\n",
       " 71,\n",
       " 144,\n",
       " 132,\n",
       " 89,\n",
       " 115,\n",
       " 267,\n",
       " 100,\n",
       " 148,\n",
       " 197,\n",
       " 627,\n",
       " 161,\n",
       " 263,\n",
       " 447,\n",
       " 97,\n",
       " 132,\n",
       " 357,\n",
       " 52,\n",
       " 329,\n",
       " 149,\n",
       " 253,\n",
       " 330,\n",
       " 549,\n",
       " 166,\n",
       " 190,\n",
       " 165,\n",
       " 202,\n",
       " 351,\n",
       " 633,\n",
       " 942,\n",
       " 123,\n",
       " 121,\n",
       " 184,\n",
       " 270,\n",
       " 139,\n",
       " 248,\n",
       " 382,\n",
       " 292,\n",
       " 215,\n",
       " 439,\n",
       " 177,\n",
       " 42,\n",
       " 173,\n",
       " 173,\n",
       " 233,\n",
       " 480,\n",
       " 377,\n",
       " 48,\n",
       " 177,\n",
       " 192,\n",
       " 84,\n",
       " 176,\n",
       " 150,\n",
       " 467,\n",
       " 210,\n",
       " 687,\n",
       " 631,\n",
       " 279,\n",
       " 136,\n",
       " 67,\n",
       " 167,\n",
       " 170,\n",
       " 359,\n",
       " 451,\n",
       " 135,\n",
       " 197,\n",
       " 1009,\n",
       " 87,\n",
       " 241,\n",
       " 492,\n",
       " 336,\n",
       " 88,\n",
       " 170,\n",
       " 425,\n",
       " 459,\n",
       " 331,\n",
       " 199,\n",
       " 158,\n",
       " 69,\n",
       " 121,\n",
       " 116,\n",
       " 120,\n",
       " 297,\n",
       " 173,\n",
       " 293,\n",
       " 103,\n",
       " 477,\n",
       " 723,\n",
       " 133,\n",
       " 413,\n",
       " 109,\n",
       " 101,\n",
       " 227,\n",
       " 141,\n",
       " 939,\n",
       " 199,\n",
       " 162,\n",
       " 377,\n",
       " 172,\n",
       " 532,\n",
       " 68,\n",
       " 41,\n",
       " 130,\n",
       " 83,\n",
       " 136,\n",
       " 787,\n",
       " 117,\n",
       " 165,\n",
       " 111,\n",
       " 124,\n",
       " 552,\n",
       " 172,\n",
       " 130,\n",
       " 96,\n",
       " 153,\n",
       " 181,\n",
       " 49,\n",
       " 119,\n",
       " 88,\n",
       " 100,\n",
       " 297,\n",
       " 363,\n",
       " 632,\n",
       " 218,\n",
       " 168,\n",
       " 100,\n",
       " 212,\n",
       " 120,\n",
       " 84,\n",
       " 135,\n",
       " 268,\n",
       " 127,\n",
       " 746,\n",
       " 127,\n",
       " 236,\n",
       " 335,\n",
       " 193,\n",
       " 843,\n",
       " 251,\n",
       " 165,\n",
       " 210,\n",
       " 239,\n",
       " 311,\n",
       " 214,\n",
       " 29,\n",
       " 334,\n",
       " 168,\n",
       " 127,\n",
       " 169,\n",
       " 110,\n",
       " 164,\n",
       " 147,\n",
       " 205,\n",
       " 133,\n",
       " 188,\n",
       " 125,\n",
       " 183,\n",
       " 326,\n",
       " 150,\n",
       " 290,\n",
       " 214,\n",
       " 328,\n",
       " 51,\n",
       " 343,\n",
       " 212,\n",
       " 454,\n",
       " 96,\n",
       " 471,\n",
       " 82,\n",
       " 523,\n",
       " 114,\n",
       " 147,\n",
       " 200,\n",
       " 387,\n",
       " 174,\n",
       " 150,\n",
       " 103,\n",
       " 187,\n",
       " 183,\n",
       " 115,\n",
       " 144,\n",
       " 131,\n",
       " 241,\n",
       " 57,\n",
       " 46,\n",
       " 23,\n",
       " 419,\n",
       " 227,\n",
       " 110,\n",
       " 91,\n",
       " 66,\n",
       " 137,\n",
       " 104,\n",
       " 164,\n",
       " 417,\n",
       " 127,\n",
       " 78,\n",
       " 181,\n",
       " 179,\n",
       " 181,\n",
       " 261,\n",
       " 243,\n",
       " 205,\n",
       " 155,\n",
       " 166,\n",
       " 180,\n",
       " 77,\n",
       " 440,\n",
       " 284,\n",
       " 328,\n",
       " 258,\n",
       " 61,\n",
       " 193,\n",
       " 39,\n",
       " 156,\n",
       " 124,\n",
       " 108,\n",
       " 322,\n",
       " 376,\n",
       " 162,\n",
       " 359,\n",
       " 139,\n",
       " 302,\n",
       " 646,\n",
       " 44,\n",
       " 253,\n",
       " 244,\n",
       " 438,\n",
       " 432,\n",
       " 226,\n",
       " 236,\n",
       " 336,\n",
       " 166,\n",
       " 444,\n",
       " 195,\n",
       " 74,\n",
       " 127,\n",
       " 213,\n",
       " 195,\n",
       " 148,\n",
       " 174,\n",
       " 148,\n",
       " 149,\n",
       " 136,\n",
       " 77,\n",
       " 53,\n",
       " 151,\n",
       " 215,\n",
       " 129,\n",
       " 196,\n",
       " 135,\n",
       " 146,\n",
       " 235,\n",
       " 166,\n",
       " 359,\n",
       " 132,\n",
       " 167,\n",
       " 141,\n",
       " 188,\n",
       " 365,\n",
       " 138,\n",
       " 118,\n",
       " 110,\n",
       " 309,\n",
       " 261,\n",
       " 127,\n",
       " 76,\n",
       " 900,\n",
       " 211,\n",
       " 273,\n",
       " 233,\n",
       " 149,\n",
       " 212,\n",
       " 302,\n",
       " 268,\n",
       " 295,\n",
       " 212,\n",
       " 143,\n",
       " 467,\n",
       " 256,\n",
       " 230,\n",
       " 129,\n",
       " 145,\n",
       " 119,\n",
       " 83,\n",
       " 322,\n",
       " 208,\n",
       " 132,\n",
       " 149,\n",
       " 278,\n",
       " 136,\n",
       " 198,\n",
       " 164,\n",
       " 349,\n",
       " 200,\n",
       " 278,\n",
       " 439,\n",
       " 130,\n",
       " 161,\n",
       " 223,\n",
       " 561,\n",
       " 397,\n",
       " 302,\n",
       " 282,\n",
       " 187,\n",
       " 192,\n",
       " 107,\n",
       " 361,\n",
       " 126,\n",
       " 201,\n",
       " 79,\n",
       " 415,\n",
       " 133,\n",
       " 300,\n",
       " 124,\n",
       " 355,\n",
       " 978,\n",
       " 323,\n",
       " 114,\n",
       " 85,\n",
       " 143,\n",
       " 221,\n",
       " 190,\n",
       " 215,\n",
       " 244,\n",
       " 626,\n",
       " 64,\n",
       " 127,\n",
       " 180,\n",
       " 128,\n",
       " 206,\n",
       " 221,\n",
       " 261,\n",
       " 122,\n",
       " 201,\n",
       " 221,\n",
       " 139,\n",
       " 315,\n",
       " 356,\n",
       " 254,\n",
       " 131,\n",
       " 128,\n",
       " 234,\n",
       " 129,\n",
       " 66,\n",
       " 134,\n",
       " 121,\n",
       " 470,\n",
       " 373,\n",
       " 129,\n",
       " 127,\n",
       " 92,\n",
       " 127,\n",
       " 223,\n",
       " 179,\n",
       " 651,\n",
       " 452,\n",
       " 935,\n",
       " 146,\n",
       " 170,\n",
       " 438,\n",
       " 94,\n",
       " 159,\n",
       " 312,\n",
       " 302,\n",
       " 128,\n",
       " 121,\n",
       " 280,\n",
       " 622,\n",
       " 166,\n",
       " 126,\n",
       " 157,\n",
       " 353,\n",
       " 45,\n",
       " 132,\n",
       " 324,\n",
       " 65,\n",
       " 304,\n",
       " 149,\n",
       " 243,\n",
       " 117,\n",
       " 719,\n",
       " 164,\n",
       " 116,\n",
       " 61,\n",
       " 368,\n",
       " 407,\n",
       " 73,\n",
       " 571,\n",
       " 170,\n",
       " 303,\n",
       " 159,\n",
       " 42,\n",
       " 400,\n",
       " 295,\n",
       " 183,\n",
       " 75,\n",
       " 215,\n",
       " 255,\n",
       " 229,\n",
       " 445,\n",
       " 1000,\n",
       " 494,\n",
       " 128,\n",
       " 580,\n",
       " 130,\n",
       " 59,\n",
       " 189,\n",
       " 526,\n",
       " 116,\n",
       " 114,\n",
       " 544,\n",
       " 118,\n",
       " 111,\n",
       " 420,\n",
       " 250,\n",
       " 258,\n",
       " 188,\n",
       " 139,\n",
       " 89,\n",
       " 135,\n",
       " 58,\n",
       " 173,\n",
       " 84,\n",
       " 150,\n",
       " 170,\n",
       " 159,\n",
       " 152,\n",
       " 170,\n",
       " 74,\n",
       " 213,\n",
       " 41,\n",
       " 250,\n",
       " 124,\n",
       " 533,\n",
       " 84,\n",
       " 186,\n",
       " 126,\n",
       " 183,\n",
       " 125,\n",
       " 153,\n",
       " 282,\n",
       " 146,\n",
       " 149,\n",
       " 215,\n",
       " 122,\n",
       " 68,\n",
       " 139,\n",
       " 128,\n",
       " 113,\n",
       " 252,\n",
       " 168,\n",
       " 434,\n",
       " ...]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[len(x) for x in x_train]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "1b4eca3e",
   "metadata": {},
   "outputs": [],
   "source": [
    "x_train=keras.preprocessing.sequence.pad_sequences(x_train,300)\n",
    "x_test=keras.preprocessing.sequence.pad_sequences(x_test,300)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef0f1edc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " 300,\n",
       " ...]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[len(x) for x in x_train]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "76bfc21e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, ..., 0, 1, 0])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "e2969ae7",
   "metadata": {},
   "outputs": [],
   "source": [
    "test='i am a student'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "ccfb6c6a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['i', 'am', 'a', 'student']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.split()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "24d7f934",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'i': 0, 'am': 1, 'a': 2, 'student': 3}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict((word,test.split().index(word)) for word in test.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4c725f74",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Metal device set to: Apple M1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-14 15:57:00.642193: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.\n",
      "2021-09-14 15:57:00.642769: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)\n"
     ]
    }
   ],
   "source": [
    "model=keras.models.Sequential()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "17d66a75",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(layers.Embedding(10000,50,input_length=300))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "91f8fb91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(25000, 300)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84a04b19",
   "metadata": {},
   "outputs": [],
   "source": [
    "25000,300,50"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "82da7d9e",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(layers.Flatten())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "8968cf4f",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(layers.Dense(128,activation='relu'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2f2ba0f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "model.add(layers.Dense(1,activation='sigmoid'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "f46dd337",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "embedding (Embedding)        (None, 300, 50)           500000    \n",
      "_________________________________________________________________\n",
      "flatten (Flatten)            (None, 15000)             0         \n",
      "_________________________________________________________________\n",
      "dense (Dense)                (None, 128)               1920128   \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 1)                 129       \n",
      "=================================================================\n",
      "Total params: 2,420,257\n",
      "Trainable params: 2,420,257\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7f9eeaaa",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/yuconggen/miniforge3/lib/python3.9/site-packages/tensorflow/python/keras/optimizer_v2/optimizer_v2.py:374: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.001),\n",
    "             loss='binary_crossentropy',\n",
    "             metrics=['acc'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "509b2641",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-14 16:07:13.289863: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)\n",
      "2021-09-14 16:07:13.293346: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz\n",
      "2021-09-14 16:07:13.446769: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/15\n",
      "97/98 [============================>.] - ETA: 0s - loss: 0.4949 - acc: 0.7357"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2021-09-14 16:07:16.170346: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:112] Plugin optimizer for device_type GPU is enabled.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98/98 [==============================] - 4s 37ms/step - loss: 0.4932 - acc: 0.7370 - val_loss: 0.3000 - val_acc: 0.8716\n",
      "Epoch 2/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 0.1585 - acc: 0.9418 - val_loss: 0.3223 - val_acc: 0.8654\n",
      "Epoch 3/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 0.0372 - acc: 0.9932 - val_loss: 0.3738 - val_acc: 0.8682\n",
      "Epoch 4/15\n",
      "98/98 [==============================] - 4s 36ms/step - loss: 0.0082 - acc: 0.9993 - val_loss: 0.4033 - val_acc: 0.8707\n",
      "Epoch 5/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 0.0028 - acc: 1.0000 - val_loss: 0.4300 - val_acc: 0.8707\n",
      "Epoch 6/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 0.0014 - acc: 1.0000 - val_loss: 0.4504 - val_acc: 0.8711\n",
      "Epoch 7/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 8.9026e-04 - acc: 1.0000 - val_loss: 0.4673 - val_acc: 0.8715\n",
      "Epoch 8/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 6.3579e-04 - acc: 1.0000 - val_loss: 0.4814 - val_acc: 0.8717\n",
      "Epoch 9/15\n",
      "98/98 [==============================] - 4s 38ms/step - loss: 4.7765e-04 - acc: 1.0000 - val_loss: 0.4939 - val_acc: 0.8712\n",
      "Epoch 10/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 3.7169e-04 - acc: 1.0000 - val_loss: 0.5055 - val_acc: 0.8716\n",
      "Epoch 11/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 2.9966e-04 - acc: 1.0000 - val_loss: 0.5148 - val_acc: 0.8710\n",
      "Epoch 12/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 2.4413e-04 - acc: 1.0000 - val_loss: 0.5239 - val_acc: 0.8711\n",
      "Epoch 13/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 2.0252e-04 - acc: 1.0000 - val_loss: 0.5328 - val_acc: 0.8718\n",
      "Epoch 14/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 1.7117e-04 - acc: 1.0000 - val_loss: 0.5406 - val_acc: 0.8718\n",
      "Epoch 15/15\n",
      "98/98 [==============================] - 4s 37ms/step - loss: 1.4590e-04 - acc: 1.0000 - val_loss: 0.5483 - val_acc: 0.8718\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<tensorflow.python.keras.callbacks.History at 0x1571029a0>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(x_train,y_train,epochs=15,batch_size=256,validation_data=(x_test,y_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da5691c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
